package com.goodving.guli.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.goodving.guli.R;
import com.goodving.guli.bt.C0668a;
import com.goodving.guli.bt.Utils;
import com.goodving.guli.bt.eventbus.EventDeviceBoundState;
import com.goodving.guli.bt.model.HwBatteryStatus;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.profile.scale.XMBLEManager;
import com.goodving.guli.model.DeviceSource;
import com.goodving.guli.model.Keeper;
import com.goodving.guli.server.WebAPI;
import com.goodving.guli.utility.Constants;
import com.loopj.android.http.AsyncHttpResponseHandler;

import cn.com.smartdevices.bracelet.config.C0886a;
import cz.msebera.android.httpclient.Header;
import de.greenrobot.event.EventBus;

/**
 * Created by john on 2015/12/4.
 */
public class UnBindActivity extends SystemBarTintActivity implements View.OnClickListener {
    private String f9396a;
    private View mUnbind;
    private View mConfirm;


    public UnBindActivity() {
        this.f9396a = Utils.f3494b;
    }

    class fj implements Runnable {
        final /* synthetic */ UnBindActivity f10235a;

        fj(UnBindActivity unBindActivity) {
            this.f10235a = unBindActivity;
        }

        public void run() {
            Utils.m7991a(this.f10235a);
            this.f10235a.setResult(-1);
            this.f10235a.finish();
        }
    }

    class fi extends AsyncHttpResponseHandler {
        final /* synthetic */ UnBindActivity f10234a;

        fi(UnBindActivity unBindActivity) {
            this.f10234a = unBindActivity;
        }

        public void onFailure(int i, Header[] headerArr, byte[] bArr, Throwable th) {
            GuliLog.m12370d("switch", "onFailure:" + i + "," + Utils.m8035b(bArr));
        }

        public void onSuccess(int i, Header[] headerArr, byte[] bArr) {
            GuliLog.m12370d("switch", "onSuccess:" + i + "," + Utils.m8035b(bArr));
        }
    }

    private void m14230a(String str) {
        int i;
        Utils.m7992a((Activity) this, (int) R.string.unbinding);
        GuliLog.m12370d("switch", "unbind device " + str);
        if (str.equals(Utils.f3495c)) {
            i = 1;
            DeviceSource.unbindWeight();
            XMBLEManager.m25163h();
            Keeper.setSyncWeightInfoToServer(0);
        } else {
//            if (C0634B.m7841a().m7862f()) {
//                C0634B.m7841a().m7849a(Keeper.readBraceletBtInfo().f19636b);
//            }
            Utils.m8053c((Context) this, 0);
            DeviceSource.unbindBracelet();
            Keeper.keepSyncTime(-1, 0);
            Keeper.keepSyncTime(-1, 3);
            Keeper.keepRealtimeStepsTimestamp(-1, 0);
            Keeper.keepRealtimeStepsTimestamp(-1, 3);
            Keeper.keepNeedBind(0);
            Keeper.setSyncBraceletInfoToServer(0);
            EventBus.getDefault().post(new HwBatteryStatus(0, 100));
            WebAPI.m11634d(C0886a.m9071f(this), C0886a.m9069d(this), new fi(this));
            i = 0;
        }
        EventBus.getDefault().post(new EventDeviceBoundState(i, 0));
        new Handler().postDelayed(new fj(this), 1);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.left_button:
                m14230a(this.f9396a);
                C0668a.m8129a((Context) this, C0668a.dQ, this.f9396a, C0668a.dC);
                break;
            case R.id.right_button:
                onHomeBackPressed();
                break;
            default:
                break;
        }
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView((int) R.layout.fragment_mili_unbind);
        String stringExtra = getIntent().getStringExtra(Constants.VIA_RESULT_SUCCESS);
        if (stringExtra != null) {
            this.f9396a = stringExtra;
        }
        mUnbind = findViewById(R.id.left_button);
        mUnbind.setOnClickListener(this);
        mConfirm = findViewById(R.id.right_button);
        mConfirm.setOnClickListener(this);

        TextView textView = (TextView) findViewById(R.id.mili_unbind_info_1);
        if (this.f9396a.equals(Utils.f3495c)) {
            textView.setVisibility(View.INVISIBLE);
            ((TextView) findViewById(R.id.mili_unbind_title_tv)).setText(R.string.weight_unbind_question);
            ((TextView) findViewById(R.id.mili_unbind_info)).setText(R.string.mili_weight_unbind_info);
            textView = (TextView) findViewById(R.id.mili_unbind_info_1);
            textView.setVisibility(View.VISIBLE);
            textView.setText(R.string.mili_weight_unbind_info_1);
            ((ImageView) findViewById(R.id.not_binded_iv)).setImageResource(R.drawable.weight_not_binded);
        } else if (!C1757t.m13470c(this)) {
            textView.setVisibility(View.INVISIBLE);
        }
    }

    protected void onHomeBackPressed() {
        C0668a.m8129a((Context) this, C0668a.dQ, this.f9396a, C0668a.dD);
        super.onHomeBackPressed();
    }

    protected void onPause() {
        super.onPause();
        C0668a.m8141b(C0668a.f3548R);
        C0668a.m8138b((Activity) this);
    }

    protected void onResume() {
        super.onResume();
        C0668a.m8133a(C0668a.f3548R);
        C0668a.m8124a((Activity) this);
    }
}
